'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac040B
    Inherits Snac

    Public Sub New()
        MyBase.New(&H4, &HB)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return 8 + 2 + 1 + _ScreenName.Length + 2 + 2 + 2 + _RequiredCapabilities.Count + 2 + 2 + 2 + 2 + _MessageText.Length
    End Function

    Private _CookieID As Long
    Public Property CookieID() As Long
        Get
            Return _CookieID
        End Get
        Set(ByVal value As Long)
            _CookieID = value
        End Set
    End Property

    Private _Channel As MessageChannel
    Public Property Channel() As MessageChannel
        Get
            Return _Channel
        End Get
        Set(ByVal value As MessageChannel)
            _Channel = value
        End Set
    End Property

    Private _ScreenName As String
    Public Property ScreenName() As String
        Get
            Return _ScreenName
        End Get
        Set(ByVal value As String)
            _ScreenName = value
        End Set
    End Property

    Private _AckReasonCode As Integer
    Public Property AckReasonCode() As Integer
        Get
            Return _AckReasonCode
        End Get
        Set(ByVal value As Integer)
            _AckReasonCode = value
        End Set
    End Property

    Private _RequiredCapabilities As New List(Of Byte)
    Public ReadOnly Property RequiredCapabilities() As List(Of Byte)
        Get
            Return _RequiredCapabilities
        End Get
    End Property

    Private _MessageText As String
    Public Property MessageText() As String
        Get
            Return _MessageText
        End Get
        Set(ByVal value As String)
            _MessageText = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        Throw New NotImplementedException
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CULng(_CookieID)))
        data.AddRange(ByteConverter.GetBytes(CUShort(_Channel)))
        data.Add(CByte(_ScreenName.Length))
        data.AddRange(ByteConverter.GetBytes(_ScreenName))

        data.AddRange(ByteConverter.GetBytes(CUShort(_AckReasonCode)))

        ' fragment identifier
        data.Add(&H5)
        ' fragment version
        data.Add(&H1)

        data.AddRange(ByteConverter.GetBytes(CUShort(_RequiredCapabilities.Count)))
        data.AddRange(_RequiredCapabilities)

        ' fragment identifier
        data.Add(&H1)
        ' fragment version
        data.Add(&H1)

        data.AddRange(ByteConverter.GetBytes(CUShort(_MessageText.Length + 2 + 2)))
        data.AddRange(New Byte() {&H0, &H0, &HFF, &HFF})

        data.AddRange(ByteConverter.GetBytes(_MessageText))

        Return data
    End Function
End Class
